﻿using System;
using System.Web.Mvc;
using Nop.Core.Data;
using Nop.Core.Infrastructure;
using Nop.Services.Logging;

namespace Spotshub.Api
{
    public class SpotshubConfig
    {
        public static void Initialize()
        {
            //initialize engine context
            EngineContext.Initialize(false);

            bool databaseInstalled = DataSettingsHelper.DatabaseIsInstalled();
            //if (databaseInstalled)
            //{
            //    //remove all view engines
            //    ViewEngines.Engines.Clear();
            //    //except the themeable razor view engine we use
            //    ViewEngines.Engines.Add(new ThemeableRazorViewEngine());
            //}


            //fluent validation
            //DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
            //ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new NopValidatorFactory()));

            //start scheduled tasks
            //if (databaseInstalled)
            //{
            //    TaskManager.Instance.Initialize();
            //    TaskManager.Instance.Start();
            //}

            //FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

            //TODO: get rid of the embedded view engine
            //register virtual path provider for embedded views
            //var embeddedViewResolver = EngineContext.Current.Resolve<IEmbeddedViewResolver>();
            //var embeddedProvider = new EmbeddedViewVirtualPathProvider(embeddedViewResolver.GetEmbeddedViews());
            //HostingEnvironment.RegisterVirtualPathProvider(embeddedProvider);

            //mobile device support
            //if (databaseInstalled)
            //{
            //    if (EngineContext.Current.Resolve<StoreInformationSettings>().MobileDevicesSupported)
            //    {
            //        //Enable the mobile detection provider (if enabled)
            //        HttpCapabilitiesBase.BrowserCapabilitiesProvider = new FiftyOne.Foundation.Mobile.Detection.MobileCapabilitiesProvider();
            //    }
            //    else
            //    {
            //        //set BrowserCapabilitiesProvider to null because 51Degrees assembly always sets it to MobileCapabilitiesProvider
            //        //which does not support our browserCaps.config file
            //        HttpCapabilitiesBase.BrowserCapabilitiesProvider = null;
            //    }
            //}

            //log application start
            if (databaseInstalled)
            {
                try
                {
                    //log
                    var logger = EngineContext.Current.Resolve<ILogger>();
                    logger.Information("API is configured", null, null);
                }
                catch (Exception)
                {
                    //don't throw new exception if occurs
                }
            }
        }
    }
}